<template>
  <div class="image-downloader">
    <el-card class="tool-card">
      <template #header>
        <div class="card-header">
          <h2>图片批量下载</h2>
          <div class="header-actions">
            <!-- 删除使用说明按钮 -->
          </div>
        </div>
      </template>
      
      <!-- 使用指南 -->
      <section class="usage-section">
        <div class="usage-wrapper">
          <div class="usage-title-section">
            <el-icon size="28" class="usage-icon"><Guide /></el-icon>
            <h2>快速使用指南</h2>
          </div>
          <div class="usage-content">
            <div class="guide-steps">
              <div class="step-item">
                <div class="step-number">1</div>
                <div class="step-content">
                  <h4>输入网址</h4>
                  <p>输入包含图片的网页URL</p>
                </div>
              </div>
              <div class="step-item">
                <div class="step-number">2</div>
                <div class="step-content">
                  <h4>提取图片</h4>
                  <p>点击"提取图片"按钮获取页面上的所有图片</p>
                </div>
              </div>
              <div class="step-item">
                <div class="step-number">3</div>
                <div class="step-content">
                  <h4>选择图片</h4>
                  <p>在列表中勾选需要下载的图片</p>
                </div>
              </div>
              <div class="step-item">
                <div class="step-number">4</div>
                <div class="step-content">
                  <h4>批量下载</h4>
                  <p>设置命名方式并下载所选图片</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>
      
      <!-- URL输入区域 -->
      <div class="url-input-area">
        <el-form :model="downloadForm" ref="formRef" :rules="rules">
          <el-form-item prop="url" label="网页URL">
            <el-input
              v-model="downloadForm.url"
              placeholder="请输入要提取图片的网页URL"
              class="url-input"
              clearable
            >
              <template #append>
                <el-button 
                  type="primary" 
                  :loading="isExtracting"
                  @click="extractImages"
                >
                  提取图片
                </el-button>
              </template>
            </el-input>
            <div class="url-examples">
              <p>示例: https://example.com/gallery</p>
            </div>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 提取结果 -->
      <div v-if="isExtracting" class="extracting-status">
        <el-progress 
          :percentage="extractingProgress" 
          :status="extractingProgress === 100 ? 'success' : ''"
          :stroke-width="15"
        >
          <span>正在提取图片...</span>
        </el-progress>
      </div>
      
      <!-- 图片列表 -->
      <div v-if="images.length > 0" class="image-list">
        <div class="list-header">
          <div class="selection-info">
            已找到 {{ images.length }} 张图片，已选择 {{ selectedImages.length }} 张
          </div>
          <div class="list-actions">
            <el-button 
              type="primary" 
              size="small" 
              @click="selectAllImages" 
              :disabled="images.length === 0"
            >
              全选
            </el-button>
            <el-button 
              type="info" 
              size="small" 
              @click="deselectAllImages" 
              :disabled="selectedImages.length === 0"
            >
              取消全选
            </el-button>
            <el-button 
              type="success" 
              size="small" 
              @click="downloadSelectedImages" 
              :disabled="selectedImages.length === 0"
            >
              批量下载 ({{ selectedImages.length }})
            </el-button>
          </div>
        </div>
        
        <el-scrollbar height="400px">
          <div class="image-grid">
            <div
              v-for="(image, index) in images"
              :key="index"
              class="image-item"
              :class="{ 'selected': isSelected(image) }"
              @click="toggleImageSelection(image)"
            >
              <div class="image-container">
                <img :src="image.url" :alt="image.name" />
                <div class="image-checkbox">
                  <el-checkbox v-model="image.selected" @click.stop />
                </div>
                <div class="image-size">{{ formatFileSize(image.size) }}</div>
              </div>
              <div class="image-name">{{ truncateFilename(image.name) }}</div>
            </div>
          </div>
        </el-scrollbar>
      </div>
      
      <!-- 下载设置 -->
      <div v-if="selectedImages.length > 0" class="download-settings">
        <h3>下载设置</h3>
        <el-form :model="downloadSettings" label-width="120px">
          <el-form-item label="文件命名">
            <el-select v-model="downloadSettings.namingOption" placeholder="选择命名方式">
              <el-option label="原始文件名" value="original" />
              <el-option label="序号命名" value="sequential" />
              <el-option label="自定义前缀 + 序号" value="custom" />
            </el-select>
            <el-input
              v-if="downloadSettings.namingOption === 'custom'"
              v-model="downloadSettings.customPrefix"
              placeholder="请输入自定义前缀"
              style="margin-top: 10px;"
            />
          </el-form-item>
          
          <el-form-item label="打包方式">
            <el-radio-group v-model="downloadSettings.packageType">
              <el-radio label="separate">单独下载</el-radio>
              <el-radio label="zip">打包为ZIP</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      
      <!-- 开发中提示 -->
      <el-alert
        title="功能开发中"
        type="warning"
        description="图片批量下载功能正在开发中，即将上线！"
        :closable="false"
        show-icon
        style="margin-top: 20px;"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { ElMessage } from 'element-plus';
import { Guide } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';

// 路由实例
const router = useRouter();

// 状态变量
const isExtracting = ref(false);
const extractingProgress = ref(0);
const images = ref([]);
const downloadForm = ref({
  url: ''
});
const downloadSettings = ref({
  namingOption: 'original',
  customPrefix: 'image',
  packageType: 'separate'
});

// 表单验证规则
const rules = {
  url: [
    { required: true, message: '请输入网页URL', trigger: 'blur' },
    { 
      type: 'url', 
      message: '请输入有效的URL', 
      trigger: 'blur' 
    }
  ]
};

// 计算属性：选中的图片
const selectedImages = computed(() => {
  return images.value.filter(img => img.selected);
});

// 检查图片是否被选中
const isSelected = (image) => {
  return image.selected;
};

// 切换图片选择状态
const toggleImageSelection = (image) => {
  image.selected = !image.selected;
};

// 全选图片
const selectAllImages = () => {
  images.value.forEach(image => {
    image.selected = true;
  });
};

// 取消全选
const deselectAllImages = () => {
  images.value.forEach(image => {
    image.selected = false;
  });
};

// 从URL提取图片
const extractImages = async () => {
  const formRef = ref(null);
  
  try {
    await formRef.value?.validate();
  } catch (error) {
    return;
  }
  
  ElMessage.warning('图片批量下载功能正在开发中，即将上线！');
  
  isExtracting.value = true;
  extractingProgress.value = 0;
  
  // 模拟提取过程
  const timer = setInterval(() => {
    extractingProgress.value += 10;
    if (extractingProgress.value >= 100) {
      clearInterval(timer);
      isExtracting.value = false;
      
      // 模拟提取结果
      images.value = Array(12).fill(null).map((_, index) => ({
        id: index,
        name: `sample-image-${index + 1}.jpg`,
        url: `https://picsum.photos/id/${30 + index}/300/200`,
        size: Math.floor(Math.random() * 1000000) + 100000, // 随机大小
        selected: false
      }));
    }
  }, 300);
};

// 下载选中的图片
const downloadSelectedImages = () => {
  ElMessage.warning('下载功能正在开发中，即将上线！');
  
  // 仅做示例，实际功能未实现
  console.log('下载设置:', downloadSettings.value);
  console.log('选中的图片:', selectedImages.value);
};

// 格式化文件大小
const formatFileSize = (bytes) => {
  if (bytes === 0) return '0 B';
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i];
};

// 截断文件名
const truncateFilename = (filename) => {
  if (filename.length <= 15) return filename;
  return filename.substring(0, 12) + '...';
};

// 升级会员
const handleUpgrade = () => {
  // 使用 Vue Router 导航到会员页面，使用命名路由而不是路径
  router.push({ name: 'membership' });
};
</script>

<style scoped>
.image-downloader {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.usage-guide {
  margin-bottom: 20px;
}

.guide-steps {
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 4px;
  margin-top: 10px;
}

.guide-steps ol {
  margin: 0;
  padding-left: 20px;
}

.guide-steps li {
  margin-bottom: 5px;
}

.url-input-area {
  margin-bottom: 20px;
}

.url-input {
  width: 100%;
}

.url-examples {
  margin-top: 5px;
  color: #909399;
  font-size: 12px;
}

.extracting-status {
  margin: 20px 0;
}

.image-list {
  margin-top: 20px;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 20px;
}

.list-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 20px;
  padding: 15px;
}

.image-item {
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
  background-color: #fff;
}

.image-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.image-item.selected {
  border: 2px solid #3498db;
}

.image-container {
  position: relative;
  width: 100%;
  height: 120px;
  overflow: hidden;
}

.image-container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-checkbox {
  position: absolute;
  top: 5px;
  right: 5px;
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 4px;
  padding: 2px;
}

.image-size {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 2px 5px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 12px;
  text-align: right;
}

.image-name {
  padding: 8px;
  font-size: 12px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.download-settings {
  display: flex;
  flex-wrap: wrap;
  gap: 30px;
}

.download-settings .el-form-item {
  min-width: 300px;
  flex: 1;
}

.download-settings h3 {
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 16px;
  color: #303133;
}

@media (max-width: 768px) {
  .list-header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .image-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }
}

.usage-section {
  margin-top: 36px;
  padding: 0 12px;
  margin-bottom: 32px;
}

.usage-wrapper {
  background-color: #f8f9fa;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  border: 1px solid #ebeef5;
}

.usage-title-section {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.usage-icon {
  color: #3498db;
}

.usage-title-section h2 {
  margin: 0;
  color: #303133;
  font-size: 20px;
  font-weight: 600;
}

.usage-content {
  display: flex;
  flex-direction: column;
}

.guide-steps {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.step-item {
  flex: 1;
  min-width: 180px;
  display: flex;
  align-items: flex-start;
  gap: 16px;
  background-color: white;
  padding: 16px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.02);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.step-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.step-number {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #3498db;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  font-weight: 600;
  flex-shrink: 0;
}

.step-content {
  flex: 1;
}

.step-content h4 {
  margin: 0 0 8px 0;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.step-content p {
  margin: 0;
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

@media (max-width: 768px) {
  .guide-steps {
    grid-template-columns: 1fr;
  }
}
</style> 